package com.gxa.community.admin.service.bill.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.community.admin.mapper.bill.BillWithdrawMapper;
import com.gxa.community.admin.service.bill.BillWithdrawService;
import com.gxa.community.core.pojo.domain.BillWithdraw;
import com.gxa.community.core.pojo.dto.bill.BillWithdrawDto;
import com.gxa.community.core.pojo.dto.bill.storage.BillWithdrawStorageDto;
import com.gxa.community.core.pojo.vo.admin.bill.BillWithdrawVo;
import com.gxa.community.core.pojo.vo.admin.bill.expand.BillWithdrawVoE;
import com.gxa.community.core.utils.LittleUtils;
import com.gxa.community.core.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author juran
 * @since 2022-07-19
 */
@Service
public class BillWithdrawServiceImpl extends ServiceImpl<BillWithdrawMapper, BillWithdraw> implements BillWithdrawService {

    @Autowired
    BillWithdrawMapper billWithdrawMapper;

    @Override
    public R queryWithdrawInfo(BillWithdrawDto billWithdrawDto,
                               long page,
                               long size) {

        Page<BillWithdraw> pageHelp = new Page<>(page, size);

        QueryWrapper<BillWithdraw> wrapper = new QueryWrapper<>();

        if (billWithdrawDto.getAgentId() != null)
            wrapper.eq("agent_id", billWithdrawDto.getAgentId());

        if (billWithdrawDto.getNo() != null)
            wrapper.eq("no", billWithdrawDto.getNo());

        //如果有起始时间那么，结束时间为当前，只有结束时间，必须有起始时间。
        if (billWithdrawDto.getStartTime() != null && billWithdrawDto.getEndTime() != null)
            wrapper.between("application_time", billWithdrawDto.getStartTime(), billWithdrawDto.getEndTime());

        if (billWithdrawDto.getStartTime() != null)
            wrapper.between("application_time", billWithdrawDto.getStartTime(), System.currentTimeMillis());

        if (billWithdrawDto.getStatus() != null && billWithdrawDto.getStatus() > 0)
            wrapper.eq("status",billWithdrawDto.getStatus());

        Page<BillWithdraw> billWithdrawPage = billWithdrawMapper.selectPage(pageHelp, wrapper);

        //提现总金额
        //查询到账佣金和未到账佣金信息
        QueryWrapper<BillWithdraw> billWithdrawQueryWrapper = new QueryWrapper<>();
        billWithdrawQueryWrapper.select("IFNULL(sum(amount),0) as amount")
                .eq("status",2);
        Map<String, Object> map =
                getMap(billWithdrawQueryWrapper);

        //返回VO
        BillWithdrawVo billWithdrawVo = new BillWithdrawVo();
        billWithdrawVo.setBillWithdrawVoList(
                billWithdrawPage.getRecords().stream().map(
                        p -> {
                            return new BillWithdrawVoE()
                                    .setTime(p.getTime())
                                    .setStatus(p.getStatus())
                                    .setApplicationTime(p.getApplicationTime())
                                    .setUserId(p.getAgentId())
                                    .setAmount(p.getAmount())
                                    .setType(p.getType())
                                    .setNo(p.getNo());
                        }
                ).collect(Collectors.toList())
        );
        //设置总提现金额
        billWithdrawVo.setWithdrawAmountStatistics((BigDecimal) map.get("amount"));

        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("total", pageHelp.getTotal());
        hashMap.put("pages", pageHelp.getPages());
        hashMap.put("pageSize", pageHelp.getSize());
        hashMap.put("currentPage", pageHelp.getCurrent());
        hashMap.put("list", billWithdrawVo);

        return R.ok().put("data", hashMap);
    }

    /**
     * 添加提现记录
     * @param billWithdrawStorageDto
     * @return
     */
    @Override
    public R addWithdrawInfo(BillWithdrawStorageDto billWithdrawStorageDto) {
        //TODO 提现流水号

        boolean save = save(new BillWithdraw()
                .setApplicationTime(System.currentTimeMillis())
                .setAgentId(billWithdrawStorageDto.getAgentId())
                .setAmount(billWithdrawStorageDto.getAmount())
                .setType(billWithdrawStorageDto.getType())
                .setNo(billWithdrawStorageDto.getNo())
                .setStatus(billWithdrawStorageDto.getStatus())
                .setTime(billWithdrawStorageDto.getTime())
                .setNo(LittleUtils.generateNo(12)));

        return save ? R.ok("添加成功") : R.error("添加失败");
    }
}
